const path = require('path');
const resolve = (dir) => path.join(__dirname, dir);
const isTong = process.env.NODE_ENV === 'tong';

const CompressionPlugin = require('compression-webpack-plugin');
const webpack = require('webpack');


// const BASE_URL = 'http://180.100.216.225:90';//测试
const BASE_URL = 'http://121.224.59.152:90';//生产环境外网
// const BASE_URL = 'http://172.16.100.203';//生产环境地址
// const BASE_URL = 'http://192.168.20.37:90';//本地

let ksiot = '/kakone';
if (isTong) {
    ksiot = '';
}
const version = new Date().getTime();

const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const cdn = {
    js: [
        {name: 'vue', src: ksiot + '/tpm/static/vue.min.js'},
        {name: 'vue-router', src: ksiot + '/tpm/static/vue-router.min.js'},
        {name: 'vuex', src: ksiot + '/tpm/static/vuex.min.js'},
        {name: 'axios', src: ksiot + '/tpm/static/axios.min.js'},
        {name: 'g2', src: ksiot + '/tpm/static/g2.min.js'},
        {name: 'swiper', src: ksiot + '/tpm/static/swiper.min.js'},
        {name: 'vue-cookies', src: ksiot + '/tpm/static/vue-cookies.js'},
        {name: 'echarts', src: ksiot + '/tpm/static/echarts.min.js'},
        {name: 'Vant', src: ksiot + '/tpm/static/vant.min.js'},
        {name: 'ViewUI', src: ksiot + '/tpm/static/iview.min.js'},
        {name: 'moment', src: ksiot + '/tpm/static/moment-with-locales.min.js'},
    ],
    css: [
        {name: 'iconfont', src: ksiot + '/tpm/fonts/common/iconfont.css'},
        {name: 'menuIcon', src: ksiot + '/tpm/fonts/menu/iconfont.css'},
        {name: 'swiper', src: ksiot + '/tpm/static/swiper.min.css'},
        {name: 'iviewUI1', src: ksiot + '/tpm/static/iview3.5.css'},
        {name: 'iviewUI', src: ksiot + '/tpm/static/iview4.7.css'},
        {name: 'viewer', src: ksiot + '/tpm/static/viewer.css'},
    ],
};

module.exports = {
    publicPath: ksiot + '/tpm/',
    outputDir: 'tpm',
    lintOnSave: false,
    productionSourceMap: false,
    chainWebpack: (config) => {
        config.module
            .rule('view-design')
            .test(/view-design.src.*?js$/)
            .use('babel')
            .loader('babel-loader')
            .end();

        config.resolve.alias.set('images', resolve('src/assets/images')).set('@', resolve('src'));
        config.when(process.title.indexOf('report') > 0, (config) => {
            config.plugin('webpack-bundle-analyzer').use(require('webpack-bundle-analyzer').BundleAnalyzerPlugin);
        });
        config.plugin('html').tap((args) => {
            args[0].cdn = cdn;
            return args;
        });

        config.externals({
            vue: 'Vue',
            'vue-router': 'VueRouter',
            vuex: 'Vuex',
            echarts: 'echarts',
            'vue-cookies': '$cookies',
            swiper: 'Swiper',
            '@antv/g2': 'G2',
            axios: 'axios',
            'view-design': 'iview',
            iview: 'iview',
            vant: 'Vant',
            moment: 'moment',
        });

        config.output.filename(`static/js/[hash].[name].${version}.js`).end();
        config.output.chunkFilename('static/js/[hash].[name].' + version + '.js').end();

        let miniCssExtractPlugin = new MiniCssExtractPlugin({
            filename: `static/css/[hash].[name].${version}.css`,
            chunkFilename: `static/css/[hash].[name].${version}.css`,
        });
        config.plugin('extract-css').use(miniCssExtractPlugin).end();

        config.module
            .rule('images')
            .use('url-loader')
            .tap((options) => {
                options.name = `static/img/[hash].[name].${version}.[ext]`;
                options.fallback = {
                    loader: 'file-loader',
                    options: {
                        name: `static/img/[hash].[name].${version}.[ext]`,
                    },
                };
                return {
                    ...options,
                    esModule: false // 关键配置：关闭 ES Module 转换
                };
            });
    },
    devServer: {
        proxy: {
            '/basic': {
                target: BASE_URL,
                // target: 'http://192.168.31.41:8881',
                changeOrigin: true,
                ws: true,
                pathRewrite: {
                    '^/basic': '/basic',
                }
            },
            '/dms': {
                target: BASE_URL,
                changeOrigin: true,
                ws: true,
                pathRewrite: {
                    '^/dms': '/dms',
                }
            },
            '/fileServer/': {
                target: BASE_URL,
                changeOrigin: true,
                ws: true,
                pathRequiresRewrite: {
                    '^/fileServer/': '/fileServer/',
                },
            },
            '/static': {
                target: BASE_URL,
                changeOrigin: true,
                ws: true,
                pathRewrite: {
                    '^/static': '/static',
                },
            },
        },
        before: (app) => {
        },
    },
    pluginOptions: {},
    configureWebpack: (config) => {
        return {};
    },
};